#!/usr/bin/python2.4
#
# Copyright 2009 Google Inc. All Rights Reserved.

"""One-line documentation for metadata module.

A detailed description of metadata.
"""

__author__ = 'arb@google.com (Anthony Baxter)'
import os, sys
from datetime import datetime

def main(files):
  dates = []
  for file in files:
    file = open(file)
    for line in file:
      if line.startswith('date: '):
        _, series, date = line.strip().split()
        date = datetime.strptime(date, "(%m/%d/%y)").date()
      if line.startswith('L2 granules:'):
        time = None
        for line in file:
          time, utc, url = line.strip().split()
        # last time is what we want
        if time:
          dates.append("%s %s"%(date, time))
  return max(dates)



if __name__ == '__main__':
  d = os.path.join(sys.argv[1], 'metadata')
  files = os.listdir(d)
  date = main(os.path.join(d, x) for x in files)
  open(os.path.join(sys.argv[1], 'datestamp.txt'), 'w').write(date + "\n")
